home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / quodlibet / player / gstbe / util.pyc (.txt) < prev   
Python Compiled Bytecode  |  2014-12-31  |  6KB  |  218 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. import os
  5. import collections
  6. from gi.repository import GLib, Gst
  7. from quodlibet.util.string import decode
  8. from quodlibet.player import PlayerError
  9.  
  10. def link_many(elements):
  11.     last = None
  12.     for element in elements:
  13.         if not last and Gst.Element.link(last, element):
  14.             return False
  15.         last = element
  16.     
  17.     return True
  18.  
  19.  
  20. def unlink_many(elements):
  21.     last = None
  22.     for element in elements:
  23.         if not last and Gst.Element.unlink(last, element):
  24.             return False
  25.         last = element
  26.     
  27.     return True
  28.  
  29.  
  30. def iter_to_list(func):
  31.     objects = []
  32.     iter_ = func()
  33.     while None:
  34.         (status, value) = iter_.next()
  35.         if status == Gst.IteratorResult.OK:
  36.             objects.append(value)
  37.             continue
  38.         break
  39.         continue
  40.         return objects
  41.  
  42.  
  43. def find_audio_sink():
  44.     '''Get the best audio sink available.
  45.  
  46.     Returns (element, description) or raises PlayerError.
  47.     '''
  48.     if os.name == 'nt':
  49.         sinks = [
  50.             'directsoundsink',
  51.             'autoaudiosink']
  52.     else:
  53.         sinks = [
  54.             'autoaudiosink',
  55.             'pulsesink',
  56.             'alsasink']
  57.     for name in sinks:
  58.         element = Gst.ElementFactory.make(name, None)
  59.         if element is not None:
  60.             return (element, name)
  61.     else:
  62.         raise PlayerError(_('No GStreamer audio sink found'))
  63.  
  64.  
  65. def GStreamerSink(pipeline_desc):
  66.     '''Returns a list of unlinked gstreamer elements ending with an audio sink
  67.     and a textual description of the pipeline.
  68.  
  69.     `pipeline_desc` can be gst-launch syntax for multiple elements
  70.     with or without an audiosink.
  71.  
  72.     In case of an error, raises PlayerError
  73.     '''
  74.     pipe = None
  75.     if pipeline_desc:
  76.         
  77.         try:
  78.             pipe = [ Gst.parse_launch(e) for e in pipeline_desc.split('!') ]
  79.         except GLib.GError:
  80.             e = None
  81.             message = e.message.decode('utf-8')
  82.             raise PlayerError(_('Invalid GStreamer output pipeline'), message)
  83.         
  84.  
  85.     if pipe:
  86.         fake = Gst.ElementFactory.make('fakesink', None)
  87.         if link_many([
  88.             pipe[-1],
  89.             fake]):
  90.             unlink_many([
  91.                 pipe[-1],
  92.                 fake])
  93.             (default_elm, default_desc) = find_audio_sink()
  94.             pipe += [
  95.                 default_elm]
  96.             pipeline_desc += ' ! ' + default_desc
  97.         
  98.     else:
  99.         (elm, pipeline_desc) = find_audio_sink()
  100.         pipe = [
  101.             elm]
  102.     return (pipe, pipeline_desc)
  103.  
  104.  
  105. class TagListWrapper(collections.Mapping):
  106.     
  107.     def __init__(self, taglist, merge = False):
  108.         self._list = taglist
  109.         self._merge = merge
  110.  
  111.     
  112.     def __len__(self):
  113.         return self._list.n_tags()
  114.  
  115.     
  116.     def __iter__(self):
  117.         for i in xrange(len(self)):
  118.             yield self._list.nth_tag_name(i)
  119.         
  120.  
  121.     
  122.     def __getitem__(self, key):
  123.         if not Gst.tag_exists(key):
  124.             raise KeyError
  125.         values = []
  126.         index = 0
  127.         while None:
  128.             value = self._list.get_value_index(key, index)
  129.             if value is None:
  130.                 break
  131.             index += 1
  132.             continue
  133.             if not values:
  134.                 raise KeyError
  135.             if self._merge:
  136.                 
  137.                 try:
  138.                     return ' - '.join(values)
  139.                 except TypeError:
  140.                     return values[0]
  141.                 
  142.  
  143.         return values
  144.  
  145.  
  146.  
  147. def parse_gstreamer_taglist(tags):
  148.     '''Takes a GStreamer taglist and returns a dict containing only
  149.     numeric and unicode values and str keys.'''
  150.     merged = { }
  151.     for key in tags.keys():
  152.         value = tags[key]
  153.         if key == 'extended-comment':
  154.             if not isinstance(value, list):
  155.                 value = [
  156.                     value]
  157.             for val in value:
  158.                 if not isinstance(val, unicode):
  159.                     continue
  160.                 split = val.split('=', 1)
  161.                 sub_key = decode(split[0])
  162.                 val = split[-1]
  163.                 if sub_key in merged:
  164.                     sub_val = merged[sub_key]
  165.                     if not isinstance(sub_val, unicode):
  166.                         continue
  167.                     if val not in sub_val.split('\n'):
  168.                         merged[sub_key] += '\n' + val
  169.                     
  170.                 merged[sub_key] = val
  171.             
  172.         if isinstance(value, Gst.DateTime):
  173.             value = value.to_iso8601_string()
  174.             merged[key] = value
  175.             continue
  176.         if isinstance(value, (int, long, float)):
  177.             merged[key] = value
  178.             continue
  179.         if isinstance(value, str):
  180.             value = decode(value)
  181.         if not isinstance(value, unicode):
  182.             value = unicode(value)
  183.         if key in merged:
  184.             merged[key] += '\n' + value
  185.             continue
  186.         merged[key] = value
  187.     
  188.     return merged
  189.  
  190.  
  191. def bin_debug(elements, depth = 0, lines = None):
  192.     '''Takes a list of gst.Element that are part of a prerolled pipeline, and
  193.     recursively gets the children and all caps between the elements.
  194.  
  195.     Returns a list of text lines suitable for printing.
  196.     '''
  197.     Colorise = Colorise
  198.     import quodlibet.util.dprint
  199.     if lines is None:
  200.         lines = []
  201.     else:
  202.         lines.append(' ' * (depth - 1) + '\\')
  203.     for i, elm in enumerate(elements):
  204.         for pad in iter_to_list(elm.iterate_sink_pads):
  205.             caps = pad.get_current_caps()
  206.             if caps:
  207.                 lines.append('%s| %s' % (' ' * depth, caps.to_string()))
  208.                 continue
  209.         name = elm.get_name()
  210.         cls = Colorise.blue(type(elm).__name__.split('.', 1)[-1])
  211.         lines.append('%s|-%s (%s)' % (' ' * depth, cls, name))
  212.         if isinstance(elm, Gst.Bin):
  213.             children = reversed(iter_to_list(elm.iterate_sorted))
  214.             bin_debug(children, depth + 1, lines)
  215.             continue
  216.     return lines
  217.  
  218.